// pages/article/index.js
const http = require('../../utils/http.js')
const config = require('../../config.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    articlesList: [],   //文章列表
    pages: 1,   //当前页数
    hasNextPage: true //是否还有下一页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.getArticleInfoTap()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function (e) {
    var that = this
    console.log("========")
    that.setData({
      articlesList: [],
      pages: 1,   //当前页数
      hasNextPage: true //是否还有下一页
    })
    that.getArticleInfoTap()
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if (!that.data.hasNextPage) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
      return
    }
    that.getArticleInfoTap()
  },
  //获取文章列表数据
  getArticleInfoTap: function() {
    var that = this
    var data = {
      limit: app.globalData.limit,
      page: that.data.pages,
    }
    http.httpReq(config.articlesList,"GET",data,function(res){
      var articlesData = app.cutImg(res.data.list)
      articlesData.forEach(function(el){
        that.data.articlesList.push(el)
      })
      that.setData({
        articlesList: that.data.articlesList,
        hasNextPage: res.data.hasNextPage,
        pages: that.data.pages + 1
      })
    })
  },
  /**
   * 点击文章进入详情页
   */
  goArticleInfoTap: function(e) {
    var articId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../index/index?articId=' + articId, //传入文章ID
    })
  }
})